package com.itheima.tliasmanager.controller;

import com.itheima.tliasmanager.annotation.Log;
import com.itheima.tliasmanager.pojo.Vo.PageResult;
import com.itheima.tliasmanager.pojo.Dto.QueryParam;
import com.itheima.tliasmanager.pojo.Vo.Result;
import com.itheima.tliasmanager.pojo.domain.Student;
import com.itheima.tliasmanager.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result pagelist(QueryParam queryParam){
        log.info("分页查询学生列表,参数:{}",queryParam);
        PageResult<Student> pageResult = studentService.pageList(queryParam);
        return Result.success(pageResult);
    }

    @Log
    @DeleteMapping("/{ids}")
    public Result batchRemoveStudent(@PathVariable List<Integer> ids){
        log.info("C--->批量删除学生,ids:{}",ids);
        studentService.batchRemoveStudent(ids);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result insertStudent(@RequestBody Student student){
        log.info("C--->添加学生,参数:{}",student);
        studentService.addStudent(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getStudentById(@PathVariable Integer id){
        log.info("C--->查询学生,id:{}",id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @Log
    @PutMapping
    public Result updateStudent(@RequestBody Student student){
        log.info("C--->更改学生,参数:{}",student);
        studentService.updateStudent(student);
        return Result.success();
    }

    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result updateStudentViolation(@PathVariable Integer id,@PathVariable Short score){
        log.info("C--->更新学生违规信息,id:{},score:{}",id,score);
        studentService.updateStudentViolation(id,score);
        return Result.success();
    }
}
